package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.service.AdminCouPonService;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/coupon/")
public class AdminCouPonController  {
     @Autowired
    AdminCouPonService adminCouPonService;

     @GetMapping("listuser")
    private Object showUserCouponList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        if (!parameterMap.containsKey("page") || !parameterMap.containsKey("limit") || !parameterMap.containsKey("sort") || !parameterMap.containsKey("order")) {
            return ResponseUtil.badArgument();
        }
        // 参数检验
        int page = 0;
        int limit = 0;
        // 检验page或者limit是不是整数
        try {
            page = Integer.parseInt(parameterMap.get("page")[0]);
            limit = Integer.parseInt(parameterMap.get("limit")[0]);
        } catch (IllegalArgumentException e) {
            return ResponseUtil.badArgumentValue();
        }
        // 检验page或者limit是不是整数，sort或者order是不是非空
        String sort = parameterMap.get("sort")[0];
        String order = parameterMap.get("order")[0];
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(sort) || StringUtils.isEmpty("order")) {
            return ResponseUtil.badArgumentValue();
        }
        // 检验order是不是asc或者desc，防止sql注入
        if (!("desc".equals(order) || "asc".equals(order))) {
            return ResponseUtil.badArgumentValue();
        }

        MarketCouponUser.Column[] columns = MarketCouponUser.Column.values();
        HashSet<String> columnStrSet = new HashSet<>();
        for (MarketCouponUser.Column column : columns) {
            columnStrSet.add(column.getValue());
        }

        if (!columnStrSet.contains(sort)) {
            return ResponseUtil.badArgumentValue();
        }

        Integer couponId = null;
        String couponIdStr = parameterMap.get("couponId") == null?null:parameterMap.get("couponId")[0];
        if(StringUtils.isEmpty(couponIdStr)){
            return ResponseUtil.badArgumentValue();
        }
        try {
            couponIdStr = couponIdStr.trim();
            couponId = Integer.parseInt(couponIdStr);
        }catch (IllegalArgumentException e){
            return ResponseUtil.badArgumentValue();
        }

        Integer userId = null;
        String userIdStr = parameterMap.get("userId") == null?null:parameterMap.get("userId")[0];
        if(!StringUtils.isEmpty(userIdStr)){
            try {
                userIdStr = userIdStr.trim();
                userId = Integer.parseInt(userIdStr);
            }catch (IllegalArgumentException e) {
                return ResponseUtil.badArgumentValue();
            }
        }

        Short status = null;
        String statusStr = parameterMap.get("status") == null?null:parameterMap.get("status")[0];
        if(!StringUtils.isEmpty(statusStr)){
            try {
                statusStr = statusStr.trim();
                status = Short.parseShort(statusStr);
            }catch (IllegalArgumentException e) {
                return ResponseUtil.badArgumentValue();
            }
        }

        List<MarketCouponUser> marketCouponUsers = adminCouPonService.queryUserCoupon(page,limit,sort,order,couponId,userId,status);
         return ResponseUtil.okList(marketCouponUsers);
    }

    @PostMapping ("create")
    private Object create(@RequestBody MarketCoupon marketCoupon) throws IOException {

        if(marketCoupon == null || marketCoupon.getName().isEmpty()){
            return ResponseUtil.badArgumentValue();
        }
        int code = adminCouPonService.createOne(marketCoupon);
        if(code == 0){
            return ResponseUtil.badArgumentValue();
        }
        return ResponseUtil.ok(marketCoupon);
    }
    @GetMapping("read")
    private Object read(int id) throws IOException {
        MarketCoupon marketCoupon = adminCouPonService.getOneCouponDetail(id);
        return ResponseUtil.ok(marketCoupon);
    }

    @PostMapping("delete")
    private Object delete(@RequestBody MarketCoupon marketCoupon) throws IOException {

        try {
            adminCouPonService.delete(marketCoupon);
        } catch (JsonProcessingException e) {
           e.printStackTrace();
           return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }
    @GetMapping("list")
    private Object list(String page,String limit,String name,String type,String status,String sort,String order) throws IOException {
        List<MarketCoupon> couponList = adminCouPonService.getCouponList(page,limit,name,type,status,sort,order);
        return ResponseUtil.okList(couponList);
    }
    @PostMapping("update")
    private Object update(@RequestBody MarketCoupon marketCoupon) throws IOException {
        if(marketCoupon == null){
            return ResponseUtil.fail();
        }
        try {
            adminCouPonService.update(marketCoupon);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(marketCoupon);
    }
}
